﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Telerik.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCF.UI.ChildWindows;
using Baosight.ZNH.ZMCF.UI.Utils;
using System.Collections.ObjectModel;

namespace Baosight.ZNH.ZMCF.UI
{
    /// <summary>
    /// 开罚款单
    /// </summary>
    /// Title:ZMCC10
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 严洪浩
    /// Version: 1.0
    /// History:
    ///       2011-06-28  严洪浩 [创建]
    ///
    public partial class ZMCF21 : EFViewCmes
    {
        PenaltyBlock _block = new PenaltyBlock();

        public ZMCF21()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
            this.INQU_fdEndDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击查看按钮执行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "罚款单-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "罚款单-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                if (!_list[0]["fdStatus"].Equals("草拟"))
                {
                    base.OpenEFWindowAlert("只能修改状态为草拟的记录！", true);
                    return;
                }
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "罚款单-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            foreach (EIRow _row in _list)
            {
                if (_row["fdStatus"] != null && !_row["fdStatus"].Equals("草拟"))
                {
                    base.OpenEFWindowAlert("只能删除状态为草拟的记录！", true);
                    return;
                }
            }
            base.OnDeleteButtonClick(sender, e);
        }

        /// <summary>
        /// 点击生效按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnReleaseButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("release", "生效", sender, e);
        }

        /// <summary>
        /// 点击已交罚款
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPenaltyButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("penalty", "已交罚款", sender, e);
        }

        /// <summary>
        /// 点击作废
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnInvalidButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            foreach (EIRow _row in _list)
            {
                if (null != _row["fdIsSubmit"] && _row["fdIsSubmit"].Equals("是"))
                {
                    base.OpenEFWindowAlert("已交罚款的记录，不能作废！", true);
                    return;
                }
            }
            base.OnModifyButtonClick("invalid", "作废", sender, e);
        }

        /// <summary>
        /// 主表弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 主表新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            Penalty _penalty = new Penalty(_dialogType);

            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.penaltyType"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.constructionUnit"));
            _eIInfo.AddBlock(_block.GetIsTrueOrFalse(false));

            _penalty.EIDataContext = new Platform.Core.UI.EIDataContext();
            _penalty.EIDataContext.Update(_eIInfo);

            if ("insert".Equals(_dialogType))
            {
                Collection<EIRow> penaltyType = this.EIDataContext.GetBlock("cmes.cl.penaltyType").GetRows();
                IEnumerable<EIRow> penaltyTypeResult = from c in penaltyType where c["display"].ToString().IndexOf("现金") >= 0 select c;
                if (penaltyTypeResult.Count() > 0)
                {
                    _penalty.fdClassify.Value = penaltyTypeResult.First()["value"].ToString();
                }
            }
            _penalty.fdSendnoteDepartment.Value = this.EIDataContext.GetAttribute("fdSendnoteDepartment");
            _penalty.fdSendnoteDepartmentId.Value = this.EIDataContext.GetAttribute("fdSendnoteDepartmentId");

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = _penalty;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

    }
}
